package acwing.区间;

import java.util.*;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/11/7
 */
public class 区间合并 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<int[]> list = new ArrayList<>();
        int[] a;
        while (n -- != 0) {
            a = new int[2];
            a[0] = in.nextInt();
            a[1] = in.nextInt();
            list.add(a);
        }
        System.out.println(merge(list));
    }

    private static int merge(List<int[]> list) {

        List<int[]> res = new ArrayList<>();
        Collections.sort(list,(Comparator.comparingInt((int[] o) -> o[0])));
        int l = Integer.MIN_VALUE;
        int r = Integer.MIN_VALUE;
        for (int[] a : list) {
            if (r < a[0]) {  // 最右边小于另一个区间的左边说明没有交集了就添加
                if (l != Integer.MIN_VALUE) {
                    res.add(new int[]{l,r});
                }
                // 更新
                l = a[0];
                r = a[1];
            } else { // 有交集
                r = Math.max(r,a[1]);
            }
        }

        if (l != Integer.MIN_VALUE) {
            res.add(new int[]{l, r});
        }
        return res.size();
    }
}
